package com.fx.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.fx.util.Constant;
import com.fx.util.JWTUtil;
import com.fx.util.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @Auther: EDZ
 * @Date: 2018-6-1 10:49:
 * @Email: 你的邮箱
 * 概要说明：该类干嘛用的
 **/
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        Cookie[] cookies = httpServletRequest.getCookies();
        String token = null;
        if(cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                String cookieName = cookie.getName();
                if ("token".equals(cookieName)) {
                    token = cookie.getValue();
                }
            }
        }
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        PrintWriter out = httpServletResponse.getWriter();
        if (StringUtils.isNotEmpty(token)) {
            JSONObject user = null;
            try {
                user = JWTUtil.parseToken(token);
            } catch (Exception e) {
                out.print(JSONObject.toJSONString(R.error("登录凭证解析失败")));
                out.flush();
                out.close();
                return false;
            }
            if (user == null || user.get("expireTime") == null) {
                out.print(JSONObject.toJSONString(R.error("登录凭证解析失败")));
                out.flush();
                out.close();
                return false;
            }
            long createdTime = user.getLong("expireTime");
            if ((createdTime + Constant.TOKEN_EXPIRE_TIME) < System.currentTimeMillis()) {
                out.print(JSONObject.toJSONString(R.error("登录已失效")));
                out.flush();
                out.close();
                return false;
            }
            httpServletRequest.setAttribute("flId", user.getString("user_key"));
            httpServletRequest.setAttribute("flOpenId", user.getString("openId"));
            httpServletRequest.setAttribute("flName", user.getString("name"));
        } else {
            out.print(JSONObject.toJSONString(R.error("请传入登录凭证")));
            out.flush();
            out.close();
            return false;
        }
        httpServletResponse.reset();
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
